﻿using SwenAssigment.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SwenAssigment
{
    public partial class AccountManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = JcUser.GetAllUser(0, "");
                GridView1.DataBind();
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = JcUser.GetAllUser(1, TextBox2.Text);
            GridView1.DataBind();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = JcUser.GetAllUser(2, TextBox3.Text);
            GridView1.DataBind();
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = JcUser.GetAllUser(4, DropDownList2.SelectedItem.Value);
            GridView1.DataBind();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = JcUser.GetAllUser(0, "");
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = JcUser.GetAllUser(3, TextBox4.Text);
            GridView1.DataBind();
        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            JcUser.LockSetting(Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString()), Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Values[1].ToString()));
            GridView1.DataSource = JcUser.GetAllUser(0, "");
            GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = e.RowIndex;
            if (GridView1.DataKeys[index].Values[2].ToString() == ((UserClass)Session["login"]).UserName)
            {
                ClientScriptManager cs = Page.ClientScript;
                cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Cannot delete the account that you are using now');", true);
            }
            else
            {
                JcUser.DeleteUser(Convert.ToInt32(GridView1.DataKeys[index].Values[0].ToString()));
                GridView1.DataSource = JcUser.GetAllUser(0, "");
                GridView1.DataBind();
            }
        }
    }
}